package com.robcity.platform.impl;

import com.robcity.platform.api.RobotLocationInfoService;
import com.robcity.platform.enums.GropEnum;
import com.robcity.platform.enums.RobNameEnum;
import com.robcity.platform.enums.TaskTypeEnum;
import com.robcity.platform.enums.WorkStatusEnum;
import com.robcity.platform.repo.RobotLocationInfoRepo;
import com.robcity.platform.util.WGS84ToGCJ02;
import com.robcity.platform.vo.RobotLocationInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class RobotLocationInfoServiceImpl implements RobotLocationInfoService {
    @Resource
    RobotLocationInfoRepo robotLocationInfoRepo;

    @Override
    public List<RobotLocationInfoVO> getRobotLocationInfo() {
        List<RobotLocationInfoVO> robotLocationInfoVOList = robotLocationInfoRepo.getRobotLocationInfo();
        robotLocationInfoVOList.forEach(robotLocationInfoVO -> {
            Duration duration = Duration.between(robotLocationInfoVO.getCreateTime(),LocalDateTime.now());
            if (duration.toMinutes() > 5) {
                robotLocationInfoVO.setOnlineStatus(0);
                robotLocationInfoVO.setOnlineStatusName("离线");
            } else {
                robotLocationInfoVO.setOnlineStatus(1);
                robotLocationInfoVO.setOnlineStatusName("在线");
            }
            if (robotLocationInfoVO.getWorkStatus() != null) {
                WorkStatusEnum workStatusEnum = WorkStatusEnum.getByCode(robotLocationInfoVO.getWorkStatus());
                robotLocationInfoVO.setWorkStatusName(workStatusEnum != null ? workStatusEnum.getDesc() : null);
            }
            if (StringUtils.isNotBlank(robotLocationInfoVO.getRobId())) {
                RobNameEnum robNameEnum = RobNameEnum.getByCode(robotLocationInfoVO.getRobId());
                robotLocationInfoVO.setRobName(robNameEnum != null ? robNameEnum.getName() : null);
                robotLocationInfoVO.setRobType(robNameEnum != null ? robNameEnum.getType() : null);
                robotLocationInfoVO.setIcon(robNameEnum != null ? robNameEnum.getIcon() : null);
            }
            if (robotLocationInfoVO.getLat() != null && robotLocationInfoVO.getLon() != null) {
                robotLocationInfoVO.setLonOrg(robotLocationInfoVO.getLon());
                robotLocationInfoVO.setLatOrg(robotLocationInfoVO.getLat());
                //返回顺序 lon lat
                double[] gcj02 = WGS84ToGCJ02.wgs84ToGcj02(robotLocationInfoVO.getLat().doubleValue(), robotLocationInfoVO.getLon().doubleValue());
                robotLocationInfoVO.setGcj02(gcj02);
                robotLocationInfoVO.setLon(BigDecimal.valueOf(gcj02[0]));
                robotLocationInfoVO.setLat(BigDecimal.valueOf(gcj02[1]));
                String formattedValue = String.format("%.3f", gcj02[0]);
                String formattedValue2 = String.format("%.3f", gcj02[1]);
                robotLocationInfoVO.setGcj02Sub(formattedValue+","+formattedValue2);
            }
            if (StringUtils.isNotBlank(robotLocationInfoVO.getTaskType())) {
                TaskTypeEnum taskTypeEnum = TaskTypeEnum.getByCode(robotLocationInfoVO.getTaskType());
                robotLocationInfoVO.setTaskTypeName(taskTypeEnum != null ? taskTypeEnum.getDesc() : null);
            }
            if (StringUtils.isNotBlank(robotLocationInfoVO.getGropId())) {
                GropEnum gropEnum = GropEnum.getByCode(robotLocationInfoVO.getGropId());
                robotLocationInfoVO.setGropIdName(gropEnum != null ? gropEnum.getDesc() : null);
            }
        });
        return robotLocationInfoVOList;
    }
}
